const app = getApp()
import { index, poster } from '../../../api/mine/invite'
import { getInfo } from '../../../utils/user'

Page({
  data: {
    theme: '',
    back: true,
    title: '邀请用户',
    loading: false,
    submitLoading: false,
    info: null,
    active: 0,
    poster: [],
    url: '',
    rule: '',
    poster_show: false, // 海报窗口
    poster_encode: '',
    count: 0
  },
  onLoad () {
    this.setData({ theme: app.global.theme })
    if (app.login()) {
      this.setData({
        loading: true,
        info: getInfo()
      })
      index().then(res => {
        const setting = res.data.setting
        this.setData({
          poster: setting.image_list,
          rule: setting.content,
          url: setting.image_list[0].url,
          count: res.data.count
        })
      })
        .finally(() => {
          this.setData({ loading: false })
        })
    }
  },
  onPullDownRefresh () { /* 监听用户下拉动作*/ },
  onReachBottom () {},

  // ===================methon================

  redirect (event) {
    app.redirect(event.currentTarget.dataset.url)
  },

  selectPoster (event) {
    const url = event.currentTarget.dataset.url
    const index = event.currentTarget.dataset.index
    this.setData({
      url: url,
      active: index
    })
  },

  // 生成海报
  onMakePoser (e) {
    if (app.login()) {
      if (!this.data.url) {
        app.toast('请选择海报')
        return false
      }
      this.setData({ submitLoading: true })
      poster(this.data.url).then(res => {
        this.setData({
          poster_encode: res.data.encode,
          poster_show: true
        })
      })
        .finally(() => {
          this.setData({ submitLoading: false })
        })
    } else {
      this.setData({ login: true })
    }
  }
})